package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.UserLoginMapper;
import com.zhijian.medical.entity.po.UserLogin;
import com.zhijian.medical.entity.vo.request.UserLoginSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.UserLoginSearchVo;
import com.zhijian.medical.entity.vo.response.UserLoginVo;
import com.zhijian.medical.service.UserLoginService;
import com.zhijian.util.AssertUtil;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 登录历史表业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-04-12 14:46:17
 */
@Slf4j
@Service
public class UserLoginServiceImpl implements UserLoginService {

    @Autowired
    private UserLoginMapper mapper;

    @Override
    public UserLogin selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public UserLoginVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(UserLogin userLogin) {
        Date now = new Date();
        userLogin.setCreateTime(now);
        userLogin.setUpdateTime(now);
        return mapper.insert(userLogin) > 0 ? userLogin.getId() : 0L;
    }

    @Override
    public boolean update(UserLogin userLogin) {
        Date now = new Date();
        userLogin.setUpdateTime(now);
        return mapper.updateByPrimaryKey(userLogin) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(UserLogin record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(UserLoginSaveOrUpdateVo saveOrUpdateVo) {
        UserLogin userLogin = new UserLogin();
        BeanUtils.copyProperties(saveOrUpdateVo, userLogin);
        return this.insert(userLogin) != 0;
    }

    @Override
    public boolean modify(UserLoginSaveOrUpdateVo saveOrUpdateVo) {
        UserLogin old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        UserLogin userLogin = new UserLogin();
        BeanUtils.copyProperties(saveOrUpdateVo, userLogin);
        return this.update(userLogin);
    }

    @Override
    public UserLoginVo selectOne(UserLoginSearchVo userLoginSearchVo) {
        return mapper.selectOne(userLoginSearchVo);
    }

    @Override
    public Integer count(UserLoginSearchVo userLoginSearchVo) {
        return mapper.count(userLoginSearchVo);
    }

    @Override
    public List<UserLoginVo> selectList(UserLoginSearchVo userLoginSearchVo) {
        return mapper.selectList(userLoginSearchVo);
    }

    @Override
    public Page<UserLoginVo> selectPage(UserLoginSearchVo userLoginSearchVo) {
        AssertUtil.check(Objects.isNull(userLoginSearchVo.getPageNum()) || Objects.isNull(userLoginSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(userLoginSearchVo);
        //分页
        Page<UserLoginVo> page = new Page<>(true, count, userLoginSearchVo.getPageNum(), userLoginSearchVo.getPageSize());
        userLoginSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<UserLoginVo> responseVos = this.selectList(userLoginSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<UserLogin> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(UserLoginSearchVo userLoginSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(userLoginSearchVo);
    }

}